### "Elitist Remedies: Complaint Resources and Representation in International Human Rights Bodies" ###
### Replication files for study 2 (individual level - China) ###
### Author: Christoph Valentin Steinert ###

# Load packages
library(tidyverse)
library(dplyr)
library(lubridate)
library(Hmisc)
library(readstata13)
library(MatchIt)
library(stringr)
library(texreg)
library(devtools)
library(sjPlot)
library(stargazer)
library(separationplot)

# Load data
rm(list = ls())
load("analysisdata_study2_china.RData")

# Prepare labels
cecc_nodup <- rename(cecc_nodup, Female = female)
cecc_nodup <- rename(cecc_nodup, Charged= charged)
cecc_nodup <- rename(cecc_nodup, Muslim = muslim)
cecc_nodup <- rename(cecc_nodup, Buddhist = buddhist)
cecc_nodup <- rename(cecc_nodup, Christian = christian)
cecc_nodup <- rename(cecc_nodup, Ethnic_Tibetan = ethnic_tibetan)
cecc_nodup <- rename(cecc_nodup, Ethnic_Uyghur = ethnic_uyghur)
cecc_nodup <- rename(cecc_nodup, Journalist = journalist)
cecc_nodup <- rename(cecc_nodup, Student = student)
cecc_nodup <- rename(cecc_nodup, Professor = professor)
cecc_nodup <- rename(cecc_nodup, Monk_Nun = monk_nun)
cecc_nodup <- rename(cecc_nodup, Farmer = farmer)
cecc_nodup <- rename(cecc_nodup, Lawyer = lawyer)
cecc_nodup <- rename(cecc_nodup, Doctor = doctor)

# Run logistic regression model
m1 <- glm(WGAD ~ Female + Charged + Muslim + Buddhist + 
            Christian + Ethnic_Tibetan +
            Ethnic_Uyghur +
            Journalist + Student +
            Professor + 
            Farmer + Lawyer +
            Doctor + City_resident,
          data = cecc_nodup, family = binomial(link = logit))

library(broom)
library(ggplot2)
results_m1 <- m1 %>% tidy()
results_m1 <- results_m1 %>% mutate(lower = estimate - 1.96*std.error, 
                                    upper = estimate + 1.96*std.error, 
                                    lower_99 = estimate -	2.576*std.error, 
                                    upper_99 = estimate +	2.576*std.error)

results_m1$term[results_m1$term == "Ethnic_Tibetan"] <- "Ethnic Tibetan"
results_m1$term[results_m1$term == "Ethnic_Uyghur"] <- "Ethnic Uyghur"
results_m1$term[results_m1$term == "City_resident"] <- "City resident"

coefs <- factor(results_m1$term, levels = rev(c("Lawyer", "Professor",
                                                 "Student",  
                                                 "Doctor", "Farmer", "Journalist", 
                                                 "City resident",
                                                 "Female", "Charged",
                                                 "Ethnic Tibetan", "Ethnic Uyghur", "Christian",
                                                 "Muslim", "Buddhist", "(Intercept)")))



### REPRODUCES FIGURE A.6 IN THE APPENDIX ###
#pdf(file = "FigureA6.pdf")
ggplot(results_m1) +
  geom_linerange(aes(ymin = lower_99, ymax = upper_99,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray40", size = 0.75) +
  geom_linerange(aes(ymin = lower, ymax = upper,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray80", size = 1.25) +
  geom_point(aes(y = estimate, x = coefs), position = position_dodge(width = 0.5), size = 2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Log odds of being covered by the UN WGAD") +
  coord_flip() +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("")
#dev.off()
stargazer(cecc_nodup)


#### Analysis with Communications-variable ####

# Prepare variable
cecc_nodup$Comm <- ifelse(is.na(cecc_nodup$Comm), 0, cecc_nodup$Comm)

# Explore relation to opinion variable
table(cecc_nodup$WGAD)
table(cecc_nodup$Comm)
table(cecc_nodup$Comm, cecc_nodup$WGAD)

# Run logistic regression model
m3a <- glm(Comm ~ Female + Charged + Muslim + Buddhist + 
             Christian + Ethnic_Tibetan +
             Ethnic_Uyghur +
             Journalist + Student +
             Professor + 
             Farmer + Lawyer +
             Doctor + City_resident,
           data = cecc_nodup, family = binomial(link = logit))
summary(m3a)

# Run logistic regression model with fixed effects for detention years
m3b <- glm(Comm ~ Female + Charged + Muslim + Buddhist + 
             Christian + Ethnic_Tibetan +
             Ethnic_Uyghur +
             Journalist + Student +
             Professor + 
             Farmer + Lawyer +
             Doctor + City_resident + factor(year),
           data = cecc_nodup, family = binomial(link = logit))
summary(m3b)

# Create coefficient plot of main model
results_m3a <- m3a %>% tidy()
results_m3a <- results_m3a %>% mutate(lower = estimate - 1.96*std.error, 
                                    upper = estimate + 1.96*std.error, 
                                    lower_99 = estimate -	2.576*std.error, 
                                    upper_99 = estimate +	2.576*std.error)

results_m3a$term[results_m3a$term == "Ethnic_Tibetan"] <- "Ethnic Tibetan"
results_m3a$term[results_m3a$term == "Ethnic_Uyghur"] <- "Ethnic Uyghur"
results_m3a$term[results_m3a$term == "City_resident"] <- "City resident"

coefs <- factor(results_m3a$term, levels = rev(c("Lawyer", "Professor",
                                                 "Student",  
                                                 "Doctor", "Farmer", "Journalist", 
                                                 "City resident",
                                                 "Female", "Charged",
                                                 "Ethnic Tibetan", "Ethnic Uyghur", "Christian",
                                                 "Muslim", "Buddhist", "(Intercept)")))


### REPRODUCES FIGURE 6 IN THE ARTICLE ###
#pdf(file = "Figure6.pdf")
#tiff("Figure6.tiff", units="in", width=5, height=5, res=1200)
ggplot(results_m3a) +
  geom_linerange(aes(ymin = lower_99, ymax = upper_99,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray40", size = 0.75) +
  geom_linerange(aes(ymin = lower, ymax = upper,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray80", size = 1.25) +
  geom_point(aes(y = estimate, x = coefs), position = position_dodge(width = 0.5), size = 2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Log odds of being covered by the UNSP") +
  coord_flip() +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("")
#dev.off()
#dev.off()

# Robustness test: Control for Issue types leading to detention
cecc_nodup$Labor_rights <- ifelse(str_detect(cecc_nodup$Issues, "Labor Rights") == T, 1, 0)
cecc_nodup$Free_expression <- ifelse(str_detect(cecc_nodup$Issues, "Freedom of Opinion and Expression") == T, 1, 0)
cecc_nodup$Property <- ifelse(str_detect(cecc_nodup$Issues, "Right to Own Property") == T, 1, 0)
cecc_nodup$Free_association <- ifelse(str_detect(cecc_nodup$Issues, "Freedom of Assembly and Association") == T, 1, 0)
cecc_nodup$Free_belief <- ifelse(str_detect(cecc_nodup$Issues, "Freedom of Religion or Belief") == T, 1, 0)
cecc_nodup$No_discrimination <- ifelse(str_detect(cecc_nodup$Issues, "Freedom from Discrimination") == T, 1, 0)
cecc_nodup$Legal_remedy <- ifelse(str_detect(cecc_nodup$Issues, "Right to Effective Legal Remedy") == T, 1, 0)

# Run logistic regression model
m3a <- glm(Comm ~ Female + Charged + Muslim + Buddhist + 
             Christian + Ethnic_Tibetan +
             Ethnic_Uyghur +
             Journalist + Student +
             Professor + 
             Farmer + Lawyer +
             Doctor + City_resident + Free_expression + Property + Labor_rights + Free_association,
           data = cecc_nodup, family = binomial(link = logit))
summary(m3a)

# Run logistic regression model with fixed effects for detention years
m3b <- glm(Comm ~ Female + Charged + Muslim + Buddhist + 
             Christian + Ethnic_Tibetan +
             Ethnic_Uyghur +
             Journalist + Student +
             Professor + 
             Farmer + Lawyer +
             Doctor + City_resident + Free_expression + Property + Labor_rights + Free_association + factor(year),
           data = cecc_nodup, family = binomial(link = logit))
summary(m3b)

# Create coefficient plot of main model
results_m3a <- m3a %>% tidy()
results_m3a <- results_m3a %>% mutate(lower = estimate - 1.96*std.error, 
                                      upper = estimate + 1.96*std.error, 
                                      lower_99 = estimate -	2.576*std.error, 
                                      upper_99 = estimate +	2.576*std.error)

results_m3a$term[results_m3a$term == "Ethnic_Tibetan"] <- "Ethnic Tibetan"
results_m3a$term[results_m3a$term == "Ethnic_Uyghur"] <- "Ethnic Uyghur"
results_m3a$term[results_m3a$term == "City_resident"] <- "City resident"
results_m3a$term[results_m3a$term == "Free_expression"] <- "Free expression"
results_m3a$term[results_m3a$term == "Free_association"] <- "Free association"
results_m3a$term[results_m3a$term == "Labor_rights"] <- "Labor rights"

coefs <- factor(results_m3a$term, levels = rev(c("Lawyer", "Professor", "City resident",
  "Free expression", "Free association", "Property", "Labor rights", "Student",  
                                                 "Doctor", "Farmer", "Journalist", 
                                                 "Female", "Charged", 
                                                 "Ethnic Tibetan", "Ethnic Uyghur", "Christian",
                                                 "Muslim", "Buddhist", "(Intercept)")))


### REPRODUCES FIGURE A.7 IN THE APPENDIX ###
#pdf(file = "FigureA7.pdf")
ggplot(results_m3a) +
  geom_linerange(aes(ymin = lower_99, ymax = upper_99,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray40", size = 0.75) +
  geom_linerange(aes(ymin = lower, ymax = upper,
                     y = estimate, x = coefs), 
                 position = position_dodge(width = 0.5), 
                 color = "gray80", size = 1.25) +
  geom_point(aes(y = estimate, x = coefs), position = position_dodge(width = 0.5), size = 2) +
  geom_hline(yintercept = 0, lty = 2) +
  xlab("") +
  ylab("Log odds of being covered by the UNSP") +
  coord_flip() +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("")
#dev.off()

# End


